﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace serie02_4_geo_obj_mousehandler
{
    class GeometricObject: System.Windows.Forms.Control
    {
        protected Pen borderPen;
        protected Pen objectPen;
        protected Rectangle innerRectangle;
        int border;
        static System.Random randomizer = new System.Random();

        public GeometricObject()
        {
            //MouseClick += MyMouseEventHandler;
            MouseClick += new MouseEventHandler(MyMouseEventHandler);
            objectPen = new Pen(Color.FromArgb(randomizer.Next(255), randomizer.Next(255),randomizer.Next(255)));
            border = randomizer.Next(30); // bis zu 20% der Gesamtbreite
            setInnerRectangle();
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);

            setInnerRectangle();

            // Pen + Rectangle
           borderPen = new Pen(Color.Red);
           Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

            // Rahmen
           e.Graphics.DrawRectangle(borderPen, rect);

            // geometrische Form
            specialPaint(e);

        }

        private void MyMouseEventHandler(object sender, EventArgs e) 
        {
            // reagiere auf Mouseclick
            objectPen = new Pen(Color.FromArgb(randomizer.Next(255), randomizer.Next(255), randomizer.Next(255)));
            this.Refresh();

        }

        private void setInnerRectangle()
        {
            int borderAbsolute = (int)((this.Width - 1) * ((double)border / 100));
            this.innerRectangle = new Rectangle(1 + borderAbsolute, 1 + borderAbsolute, this.Width - 3 - 2 * borderAbsolute, this.Height - 3 - 2 * borderAbsolute);
        }
        protected virtual void specialPaint(System.Windows.Forms.PaintEventArgs e) { }
    }
}

